import { cateList, oneCate, changeCate, addCate, delCate } from "../../request/api";
import { successAlert } from '../../utils/alert'
const state = {
    tableData: [],//列表
    form: {
        pid: "", //上级分类编号
        catename: "", //商品分类名称
        img: "",
        status: 1 //状态1正常2禁用
    },
}

// 2,声明mutations
const mutations = {
    // 专门来修改goods数据
    changecate(state, obj) {
        state.tableData = obj;
    },
    editcate(state, form) {
        state.form = form
    },
    addcate(state, a) {
        state.form = a
    },
    cancelcate(state, b) {
        state.form = b
    },
    delcate(state,list){
        state.tableData=list
    }
}

// 3.声明actions
const actions = {
    CateListActions(context) {//请求数据列表
        cateList({ istree: true }).then(res => {
            if (res.code == 200) {
                // console.log(res.list);
                context.commit('changecate', res.list)
            }
        })
    },
    editActions(context, id) {
        oneCate({ id }).then(res => {
            if (res.code == 200) {
                // console.log(res.list);
                res.list.id = id
                context.commit('editcate', res.list)
            }
        })
    },
    addActions(context) {
        let a = {
            pid: "", //上级分类编号
            catename: "", //商品分类名称
            img: "",
            status: 1 //状态1正常2禁用
        }
        addCate(state.form).then(res => {
            if (res.code == 200) {
                successAlert(res.msg);
                context.commit('addcate', a)
                context.dispatch('CateListActions')
            }
        });
    },
    cancelAction(context) {
        let b = {
            pid: "", //上级分类编号
            catename: "", //商品分类名称
            img: "",
            status: 1 //状态1正常2禁用
        }
        context.commit('cancelcate', b)
    },
    changeAction(context) {
        changeCate(state.form).then(res => {
            if (res.code == 200) {
                // console.log(res);
                successAlert(res.msg);
                // context.commit('changecate')
            }
        })
    },
    delActions(context,id) {

        delCate({ id }).then(res => {
            if (res.code == 200) {
                // console.log(res);
                successAlert(res.msg);
                context.commit('delcate',res.list)
            }
        })
    }

}
// 4.声明getters
const getters = {
    tableData(state) {
        return state.tableData
    },
    form(state) {
        return state.form
    },
}

export default {
    state,
    mutations,
    actions, getters,
    namespaced: true,//使用命名
}